package database.models;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashSet;

import twitter4j.HashtagEntity;
import twitter4j.Status;

import database.objects.DatabaseObject;

/**
 * The model for inserting a tweet. Is called when different objects need to be
 * inserted, like message, owner and tags.
 * 
 * @author Project group 9
 * 
 */
public class TweetModel extends Model {

	private MessageModel messageModel;
	private MessageOwnerModel messageOwnerModel;
	private TagModel tagModel;

	/**
	 * A tweet consist of a Message and a MessagOwner
	 * 
	 * @param connection
	 *            The database connection
	 * @param MessageModel
	 *            The model for message
	 * @param MessageOwnerModel
	 *            The model for the owner of a message.
	 * @param TagModel
	 *            The model for the tags of a message.
	 */
	public TweetModel(Connection con, MessageModel messageModel,
			MessageOwnerModel messageOwnerModel, TagModel tagModel) {
		super(con);
		this.messageModel = messageModel;
		this.messageOwnerModel = messageOwnerModel;
		this.tagModel = tagModel;
	}

	/**
	 * @deprecated Empty shell.
	 */
	@Deprecated
	@Override
	public ArrayList<DatabaseObject> load(String column, long id, String value) {
		return null;
	}

	/**
	 * Method that saves a tweet into the database and everything that comes
	 * with that. The owner is saved if he doesn't allready exist. The message
	 * is saved. The Tags are saved. tags are linked to a message.
	 * 
	 * @param Status
	 *            The status of a message.
	 */
	public void save(Status status) {
		// Save a messageOwner and a message.
		if (messageOwnerModel.byTwitterId(status.getUser().getId()).getId() == -1) {
			messageOwnerModel.save(status);
		}
		messageModel.save(status);
		// Save all the tags.
		HashSet<String> newTags = new HashSet<String>();
		// Get all the tags with #
		for (HashtagEntity hashtag : status.getHashtagEntities()) {
			newTags.add(hashtag.getText().toLowerCase());
		}
		// Get all the tags from the sentence that are all ready in the
		// database. Makes it incredible slow.
		//for (String newTag : analyseSentence(status.getText())) {
		//	newTags.add(newTag);
		//}
		
		// Add the new tags, and link all the tags to messages.
		for (String tag : newTags) {
			if (tagModel.byName(tag).getId() == -1) {
				tagModel.save(status, tag, true);
				
			}
			tagModel.linkMessageTag(status, tag);
		}
	}

	/**
	 * This method analysis a sentence and looks for words that don't have # in
	 * front of it, but are tags in the database.
	 * 
	 * @param text
	 *            The text in a sentence
	 * @return ArrayList<String> The words that are tags in the database.
	 */
	public HashSet<String> analyseSentence(String text) {
		String[] words = text.toLowerCase().split(" ");
		HashSet<String> newTags = new HashSet<String>();

		for (String word : words) {
			if (!word.startsWith("@") && (!word.startsWith("#"))) {
				if (tagModel.byName(word).getId() != -1) {
					newTags.add(word);
				}
			}
		}
		return newTags;
	}
}
